package com.woniuxy.woniuhis.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.service.EmpService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.VerifyCode;
import com.woniuxy.woniuhis.util.WebUtils;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class UserController {

	@Autowired
	EmpService empService;

	/**
	 * 登录
	 */
	@ResponseBody
	@RequestMapping("/login")
	public ResponseJson<Emp> userLogin(String userName,String userPassword,String verifyCode) {
		// 返回页面登录信息
		ResponseJson<Emp> respJson = new ResponseJson<Emp>();
		if (userName == null || "".equals(userName) || userPassword == null || "".equals(userPassword)) {
			respJson.setCode(500);
			respJson.setMsg("用户名，密码不能为空");
			return respJson;
		}
		String sessionVerifyCode = (String) WebUtils.getSession().getAttribute("session_verifycode");
		log.info(sessionVerifyCode + "\t" + verifyCode);
		if (verifyCode == null || "".equals(verifyCode)) {
			respJson.setCode(500);
			respJson.setMsg("验证码不能为空");
			return respJson;
		} else if (!verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
			respJson.setCode(500);
			respJson.setMsg("验证码不正确");
			return respJson;
		}

		try {
			Emp emp = empService.findEmpByEmpName(userName);
			if (emp != null && emp.getEmpPassword().equals(userPassword)) {
				WebUtils.getSession().setAttribute("user", emp);
				respJson.setCode(200);
				respJson.setMsg("登录成功");
			} else {
				respJson.setCode(500);
				respJson.setMsg("用户名或密码错误");
			}
			return respJson;
		} catch (Exception e) {
			e.printStackTrace();
			respJson.setMsg(e.getMessage());
			return respJson;
		}
	}

	/**
	 * 用户注册
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/register")
	public ResponseJson<Emp> userReg(HttpServletRequest req) {
		String userName = req.getParameter("userName");
		String userPassword = req.getParameter("userPassword");
		String rePassword = req.getParameter("rePassword");
		// 储存错误信息
		ResponseJson<Emp> respJson = new ResponseJson<Emp>();
		try {
			if (userPassword.length() < 6) {
				respJson.setCode(500);
				respJson.setMsg("请设置大于6位的密码");
				return respJson;
			} else if (!userPassword.equals(rePassword)) {
				respJson.setCode(500);
				respJson.setMsg("两次输入密码不一致");
				return respJson;
			}
			Emp emp = new Emp();
			emp.setEmpName(userName);
			emp.setEmpPassword(userPassword);
			empService.addEmp(emp);
			respJson.setCode(200);
			respJson.setMsg("注册成功");
			return respJson;
		} catch (Exception e) {
			e.printStackTrace();
			respJson.setCode(500);
			respJson.setMsg(e.getMessage());
			return respJson;
		}

	}

	/**
	 * 生成验证码，返回前端
	 * 
	 * @throws IOException
	 */
	@RequestMapping("/generateVerifyCode")
	public void generateVerifyCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		VerifyCode vc = new VerifyCode();
		// 需先生成图片，在获取文本
		BufferedImage image = vc.getImage();
		String text = vc.getText();
		// 在session中保存验证码
		req.getSession().setAttribute("session_verifycode", text);
		// 用字节流写出
		VerifyCode.output(image, resp.getOutputStream());
	}
}
